import SwiftUI
import SPGlassKit

public struct GlassPromptBarExample: GlassExample {
    public static let componentID = "glass-prompt-bar"
    @State private var draft = ""
    @State private var isResponding = false
    @State private var sent: [String] = []
    @State private var attachments = [
        GlassPromptAttachment(id: "a1", name: "sample-report.pdf", systemImage: "doc.richtext"),
        GlassPromptAttachment(id: "a2", name: "a-sample-image-with-a-rather-long-file-name.png", systemImage: "photo"),
    ]
    @State private var nextAttachment = 3

    public init() {}

    public var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Sample data · nothing is sent anywhere; prompts are only listed below.")
                .font(.caption)
                .foregroundStyle(.secondary)
            ScrollView {
                VStack(alignment: .leading, spacing: 8) {
                    ForEach(Array(sent.enumerated()), id: \.offset) { _, prompt in
                        Text(prompt)
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                    if isResponding {
                        GlassThinkingIndicator(.writing, label: "Simulating a response")
                    }
                }
            }
            GlassPromptBar(text: $draft, placeholder: "Ask the sample assistant", isResponding: isResponding, attachments: attachments) { prompt in
                sent.append(prompt)
                isResponding = true
            } onStop: {
                isResponding = false
            } onAttach: {
                // A real host presents its own file importer or photo picker here.
                attachments.append(GlassPromptAttachment(id: "a\(nextAttachment)", name: "sample-\(nextAttachment).txt"))
                nextAttachment += 1
            } onRemoveAttachment: { attachment in
                attachments.removeAll { $0.id == attachment.id }
            }
        }
        .padding()
        .task(id: isResponding) {
            guard isResponding else { return }
            try? await Task.sleep(for: .seconds(3))
            if !Task.isCancelled { isResponding = false }
        }
    }
}

#Preview("GlassPromptBar") { GlassPromptBarExample().glassTheme(.aurora) }
